<?PHP

$user = new user(USER_ID); 
$user_id = USER_ID;
$user_info[$user_id] = $user->load_tables('user_settings,user_stats,user_gladiator');

$item_id = (int) @$_REQUEST['function'];

echo display_text('wep_insp');


if ($item_id > 0)
{
	$select_info = $db->return_first_result("SELECT user_items.*,gladiator_items.name FROM `user_items` LEFT JOIN `gladiator_items` ON user_items.item = gladiator_items.id WHERE user_items.type='weapon' AND user_items.assoc_id='$user_id' AND user_items.item='$item_id' AND user_items.quantity > '0'");
	
	if ($select_info['quantity'] > 0)
	{
		$select_name = $select_info['name'];
		$select_location = $select_info['purchase_location'];
		$db->query("UPDATE `user_items` SET `quantity`=quantity-'1' WHERE `item`='$item_id' AND `section`='gladiator' AND `type`='weapon' AND `assoc_id`='$user_id'");
		if (dice_throw($select_info['quality'],100))
		{
			# Weapon passed, move it into inventory
			$db->query("UPDATE `user_gladiator` SET `gladiator_items_inventory` = CONCAT( gladiator_items_inventory, ',$item_id,' ) WHERE `assoc_id`='".USER_ID."'");
			$output = "The $select_name you bought \nfrom $select_location \n\nPASSED\n\nthe inspection and is now available \nfor you to use (equip) \nin the gladiator games.";
		}
		else 
		{
			# Weapon failed inspection, delete it (already done above)
			$output = "The $select_name you bought \nfrom $select_location \n\n<b>failed</b> \n\nthe inspection and has been put out of commision/scrapped.";
		}
		echo "<pre>$output</pre>";
	}
	echo "<PRE>You can't re-inspect a weapon.</PRE>";
}



$query = "SELECT user_items.*,gladiator_items.name FROM `user_items` LEFT JOIN `gladiator_items` ON user_items.item = gladiator_items.id WHERE user_items.type='weapon' AND user_items.assoc_id='$user_id' AND user_items.item='$item_id' AND user_items.quantity > '0'";
if ($result = $db->query($query)) {
    
    $num_results = $result->num_rows;
    if ($num_results > 0)
    {
        echo '<h2>Non-inspected Weapons ('.$num_results.')</h2><p>';
        
        $wep_info = array();
        while ($row = $result->fetch_assoc()) {
             $wep_id = $row['item'];
             $wep_info["/wep_insp/$wep_id/"] =  $row['name'];
        }
        echo player_options($wep_info);
    }
    else
    {
        echo '<h2>Non-inspected Weapons</h2><p>';
        echo '<I>No weapons to be inspected.</I>';
    }
    /* free result set */
    $result->close();
}

?>